1. Introduction to Robots

1.1 Introduction to Agent-Based Modeling

CS371: Cognitive Science
Bryn Mawr College
Doug Blank

1.2 Ladybug Robot Simulation

Our first simulation is a discrete simulation: the world is divided into discrete locations (called patches) and the world operates in discrete time steps.

A Ladybug world looks like this:

You will be writing a brain for the Ladybug, the black and red circle in the middle of the world. In the above image, ladybug is facing up. Ladybug can:

  • turn left or right, but only in a grid-like manner
  • go forward or backward specific distances
  • sense the world immediately in front of it, and in its peripheral view

1.3 Writing a Brain

Brains for the Ladybug Simulation are composed of a set of rules. You can have as many rules as you wish.

Rules are composed of the following parts, separated by spaces:

STATE    SENSE-PATTERN   ->    ACTION     NEXT-STATE

In your brain, you can also have comments (lines that start with a #) and blank lines. Here is a very simple brain:

# This is my first brain!

0         *****          ->    forward(1)  0

Next, let's examine each part of a rule to find out what this does.

1.3.1 STATE and NEXT-STATE

State is a name used as a simple memory for your ladybug. Ladybugs always start out in state "0". States can be any name with no spaces.

1.3.2 SENSE-PATTERN

Ladybugs can see in the 5 cells near the front of its head. In the following pictures, the ladybug can see the patches with a yellow circle in the, and cannot see the patches that are just green.

Facing up:

Facing right:

Facing down:

Facing left:

In the simulator, the ladybug is a bit bigger than its nearby cells, so it overlaps just a little, as you can see here:

The 5 sense locations are matched in the same locations ego-centrally located to the ladybug. So they are always listed in this order:

left side in front/left center/forward in front/right right side

When you write a rule, you can either match a patch exactly, or you can use the "wildcard" character *.

  • f - food
  • w - wall
  • * - either food, wall, or open space

1.3.3 ACTION

  • forward(DISTANCE) - where DISTANCE is between 1 and 9, inclusive
  • backward(DISTANCE) - where DISTANCE is between 1 and 9, inclusive
  • turnLeft(DEGREE) - where DEGREE is 90 or 180
  • turnRight(DEGREE) - where DEGREE is 90 or 180
  • stop()
0    *****    ->    forward(1)    0

Finally, in order to run your rules in the simulation, we must use a little magic.

%%simulation

# My first brain!

0 ***** -> forward(1) 0

1.4 Putting it all together

Note: ordering is important! Rules are checked for matches from top to bottom.

Your agent has an energy level. It starts at 100, and decreases with the amount of energy it expends. If it gets down to zero, it dies.

Some notes about the world:

  • on each restart, the food is randomly placed in the world
  • the walls appear as purple circles
In [1]:
%%simulation

# My first brain!

0 ***** -> forward(1) 0
In [1]:
%%simulation

0 **w** -> turnRight(90)  0
0 ***** -> forward(1)     0

1.5 Analysis

Let's analyze the last ladybug's run. We can do a quick plot of the energy over time.

First, we import the simulation library in order to get to the ladybug robot:

In [2]:
from calysto.simulation import get_robot
In [3]:
ladybug = get_robot()

The ladybug keeps track of its energy level at each step in ladybug.history:

In [7]:
str(ladybug.history)
Out[7]:
'[100, 99.0, 119.0, 118.0, 117.0, 116.0, 115.0, 114.0, 113.0, 112.0, 111.0, 110.0, 109.0, 108.0, 107.0, 106.0, 105.0, 104.0, 103.0, 102.0, 101.0, 100.0, 99.25, 98.25, 97.25, 96.25, 95.25, 94.25, 93.25, 92.25, 91.25, 90.25, 89.25, 88.25, 87.25, 86.25, 85.25, 84.25, 83.25, 82.25, 81.25, 80.25, 79.25, 78.25, 77.25, 76.25, 75.25, 74.25, 73.25, 72.25, 71.25, 70.25, 69.5, 68.5, 67.5, 66.5, 65.5, 64.5, 63.5, 62.5, 61.5, 60.5, 59.5, 58.5, 57.5, 56.5, 55.5, 54.5, 53.5, 52.5, 51.5, 50.5, 49.5, 48.5, 47.5, 46.5, 45.5, 44.5, 43.5, 42.5, 41.5, 40.5, 39.5, 38.5, 37.5, 36.5, 35.5, 34.5, 33.5, 32.5, 31.5, 30.75, 29.75, 28.75, 27.75, 26.75, 25.75, 24.75, 23.75, 22.75, 21.75, 20.75, 19.75, 18.75, 17.75, 16.75, 15.75, 14.75, 13.75, 12.75, 11.75, 10.75, 9.75, 8.75, 7.75, 6.75, 5.75, 4.75, 3.75, 2.75, 1.75, 0.75, -0.25]'

What was the maximum energy level over the run?

In [5]:
max(ladybug.history)
Out[5]:
119.0

Finally, we can create a quick visualization of the energy level over time:

In [6]:
%matplotlib inline

import matplotlib.pyplot as plt

plt.plot(ladybug.history)
plt.ylabel("Energy")
plt.xlabel("Time")
plt.show()